package com.shaoshaohuo.app.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.DetailsEntity;
import com.shaoshaohuo.app.ui.DetailsActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/22.
 */

public class NewDetailsFragment extends Fragment {
    private List<Fragment> list = new ArrayList<>();
    private MyBarocast myBarocast;
    private ViewPager frg_newdetails_vp;

    public static NewDetailsFragment newInstance(Serializable s) {
        Bundle args = new Bundle();

        args.putSerializable("bottom", s);
        NewDetailsFragment fragment = new NewDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.frgmrnt_newdetails, null);
        myBarocast = new MyBarocast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("tabselter");
        getActivity().registerReceiver(myBarocast,intentFilter);
        return inflate;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unregisterReceiver(myBarocast);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        DetailsEntity bottom = (DetailsEntity) arguments.getSerializable("bottom");
        list.add(DetailsFragmentBottom.newInstance(bottom));
        String catid = bottom.getData().getCatid();
        list.add(DetailsFragmentBottomTow.newInstance("catid"));
        initview(view);
    }

    private void initview(View view) {
        frg_newdetails_vp = (ViewPager) view.findViewById(R.id.frg_newdetails_vp);
        Myadapter myadapter = new Myadapter(getChildFragmentManager());
        frg_newdetails_vp.setAdapter(myadapter);
        frg_newdetails_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                ((DetailsActivity)getActivity()).setvpseleter(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    class Myadapter extends FragmentPagerAdapter {

        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
    class MyBarocast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        if(action.equals("tabselter")){
            int postion = intent.getIntExtra("postion", 0);
            frg_newdetails_vp.setCurrentItem(postion);
        }
        }
    }
}
